home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / vol7n3.arc / PP703.ARC / ATOI.ASM next >
Assembly Source File  |  1988-01-08  |  3KB  |  88 lines

  1.         name    atoi
  2.         page    55,132
  3.         title   ATOI - ASCII to integer
  4.  
  5. ;
  6. ; ATOI.ASM - convert ASCII string to 
  7. ;            16-bit decimal integer.
  8. ;
  9. ; Copyright 1987 Ziff Communications Co.
  10. ; Ray Duncan
  11. ; Call with:    DS:SI = address of string
  12. ;
  13. ;               where 'string' is in the form
  14. ;               
  15. ;                  [whitespace][sign][digits]
  16. ;
  17. ; Returns:      AX    = result
  18. ;               DS:SI = address+1 of terminator 
  19. ;
  20. ;               other registers preserved
  21. ;
  22. ; Like the C library 'atoi', this routine gives no 
  23. ; warning in the event of overflow, and terminates 
  24. ; on the first invalid character.
  25. ;
  26.  
  27. blank   equ     20h             ; ASCII blank character
  28. tab     equ     09h             ; ASCII tab character
  29.  
  30. _TEXT   segment word public 'CODE'
  31.  
  32.         assume  cs:_TEXT
  33.  
  34.         public  atoi
  35. atoi    proc    near            ; ASCII to 16-bit integer
  36.  
  37.         push    bx              ; save registers
  38.         push    cx
  39.         push    dx
  40.  
  41.         xor     bx,bx           ; initialize forming answer
  42.         xor     cx,cx           ; initialize sign flag
  43.  
  44. atoi1:  lodsb                   ; scan off whitespace
  45.         cmp     al,blank        ; ignore leading blanks
  46.         je      atoi1
  47.         cmp     al,tab          ; ignore leading tabs
  48.         je      atoi1
  49.  
  50.         cmp     al,'+'          ; if + sign proceed
  51.         je      atoi2
  52.         cmp     al,'-'          ; is it - sign?
  53.         jne     atoi3           ; no, test if numeric
  54.         dec     cx              ; was - sign, set flag
  55.                                 ; for negative result
  56.  
  57. atoi2:  lodsb                   ; get next character
  58.  
  59. atoi3:  cmp     al,'0'          ; is character valid?
  60.         jb      atoi4           ; jump if not '0' to '9'
  61.         cmp     al,'9'
  62.         ja      atoi4           ; jump if not '0' to '9'
  63.  
  64.         and     ax,0fh          ; isolate lower four bits
  65.  
  66.         xchg    bx,ax           ; multiply answer x 10
  67.         mov     dx,10
  68.         mul     dx
  69.  
  70.         add     bx,ax           ; add this digit
  71.  
  72.         jmp     atoi2           ; convert next digit
  73.  
  74. atoi4:  mov     ax,bx           ; result into AX
  75.         jcxz    atoi5           ; jump if sign flag clear
  76.         neg     ax              ; make result negative
  77.  
  78. atoi5:  pop     dx              ; restore registers
  79.         pop     cx
  80.         pop     bx
  81.         ret                     ; back to caller
  82.  
  83. atoi    endp
  84.  
  85. _TEXT   ends
  86.         end
  87.